package com.example.planetexploder.model.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.constants.SPConst;
import com.example.planetexploder.constants.SortConst;


/**
 * 应用设置操作类
 * @author 蒋子涵
 * 2019.10.15
 */
public class AppSettings implements SPConst, SortConst {

    private Context mContext;

    public AppSettings(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 储存首页显示的书的ID
     * @param bookId    摘抄集的id
     */
    public void set_FirstPageBook(String bookId) {
        Log.d(LogConst.MTAG, "set_FirstPageBook()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(FIRST_PAGE_BOOKID, bookId);
        editor.apply();
    }

    /**
     * 获取首页显示的书的ID
     * @return          首页摘抄集的id
     */
    public String get_FirstPageBook() {
        Log.d(LogConst.MTAG, "get_FirstPageBook()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        return sp.getString(FIRST_PAGE_BOOKID,"");
    }

    /**
     * 储存设置的主题颜色
     * @param colorId   主题颜色的编号
     */
    public void set_ThemeColor(int colorId) {
        Log.d(LogConst.MTAG, "set_ThemeColor()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(THEME_COLOR, colorId);
        editor.apply();
    }

    /**
     * 获取设置的主题颜色
     * @return          主题颜色的编号
     */
    public int get_ThemeColor() {
        Log.d(LogConst.MTAG, "get_ThemeColor()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        return sp.getInt(THEME_COLOR,0);
    }

    /**
     * 储存设置的排序方式
     * @param sortWay   分类方式的编号
     */
    public void set_SortWay(int sortWay) {
        Log.d(LogConst.MTAG, "set_SortWay()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(SORTWAY, sortWay);
        editor.apply();
    }

    /**
     * 获取设置的排序方式
     * @return          分类方式的编号
     */
    public int get_SortWay() {
        Log.d(LogConst.MTAG, "get_SortWay()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        return sp.getInt(SORTWAY,1);
    }

    /**
     * 判断是否是第一次打开
     * @return          是否是第一次打开
     */
    public int isFirstTime() {
        Log.d(LogConst.MTAG, "isFirstTime()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        return sp.getInt(IS_FIRST_TIME,0);
    }

    /**
     * 设置不是第一次打开
     */
    public void set_SecondTime() {
        Log.d(LogConst.MTAG, "set_SecondTime()");
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(IS_FIRST_TIME, 1);
        editor.apply();
    }

    /**
     * 设置tag1
     * @param tag1  tag1
     */
    public void set_Tag1(int tag1) {
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(TAG1, tag1);
        editor.apply();
    }

    /**
     * 获得tag1
     * @return  tag1
     */
    public int get_Tag1() {
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        return sp.getInt(TAG1,0);
    }

    /**
     * 设置tag2
     * @param tag2  tag2
     */
    public void set_Tag2(int tag2) {
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(TAG2, tag2);
        editor.apply();
    }

    /**
     * 获得tag2
     * @return  tag2
     */
    public int get_Tag2() {
        SharedPreferences sp = mContext.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
        return sp.getInt(TAG2,0);
    }
}
